package com.ustadzmenjawab.crawler;

import java.io.IOException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.ustadzmenjawab.shared.Article;

public class PercikanIman extends CrawlerRoot{
	public static final String HOME_URL = "http://www.percikaniman.org/category/tanya-jawab-islam";
	public static final String PATTERN = "percikaniman.org/category/tanya-jawab-islam";
	public static void main (String[] args){
		try {
			int maxListPage = 24;
			PercikanIman cr = new PercikanIman();
			for(int i = 1; i <= maxListPage;i++){
				Document doc = Jsoup.connect(HOME_URL+"/"+i).timeout(0).get();
				Elements articlesLs = doc.select("a[href*="+PATTERN+"]");
				for(Element link:  articlesLs){
					try{
						String absHref = link.attr("abs:href");
						if(cr.isRelevantURL(absHref)){
							System.out.print(absHref);
							Document page = Jsoup.connect(absHref).timeout(0).get();
							String content = page.body().html();
							if(cr.isRelevantPage(content)){
								System.out.println(" is a relevant page");
								if(!cr.isUrlExist(absHref)){
									cr.insertContent(absHref, content);
								}
							}else{
								System.out.println(" is not a relevant page");
							}
						}
					}catch(Exception e){
						System.out.println(" exception "+e.getClass() +" :" + e.getMessage());
						e.printStackTrace();
					}
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	public PercikanIman(){
		super();
	}
	public boolean insertContent(String url, String content) {
		try{
			Article article = new Article();
			String title = getTitle(content);
			if(isTitleExist(title, "percikaniman.org")){
				return false;
			}
			article.setJudul(getTitle(content));
			article.setKategori("shariah");
			article.setSumber("percikaniman.org");
			article.setTahun("2012");
			article.setUrl(url);
			article.setUstadz("Dr. Aam Amiruddin");
			article.setArtikel(getArticle(content));
			article.setPertanyaan(getQuestion(content));
			db.insertArticle(article);
			return true;
		}catch(Exception e){
			System.out.println(" exception "+e.getClass() +" :" + e.getMessage());
			
			e.printStackTrace();
			insertFailure(url,"exception "+e.getClass() +" :" + e.getMessage());
			return false;
		}
	}

	
	
	/* (non-Javadoc)
	 * @see com.ustadzmenjawab.crawler.Crawler#isRelevantURL(java.lang.String)
	 */
	@Override
	public boolean isRelevantURL(String url){
		if(url.contains("/category/tanya-jawab-islam")){
			return true;
		}
		return false;
	}
	/* (non-Javadoc)
	 * @see com.ustadzmenjawab.crawler.Crawler#isRelevantPage(java.lang.String)
	 */
	@Override
	public boolean isRelevantPage(String content){
		if(content.contains("class=\"text\"")){
			return true;
		}
		return false;
	}

	/* (non-Javadoc)
	 * @see com.ustadzmenjawab.crawler.Crawler#getTitle(java.lang.String)
	 */
	@Override
	public String getTitle(String body){
		Document doc = Jsoup.parse(body);
		Element p = doc.select("h6").first();
		return p.text();
	}

	/* (non-Javadoc)
	 * @see com.ustadzmenjawab.crawler.Crawler#getQuestion(java.lang.String)
	 */
	@Override
	public String getQuestion(String body){
		Document doc = Jsoup.parse(body);
		Element p = doc.select("div[class*=text]").first();
		if(p != null){
			Element q = p.select("strong").first();
			if(q != null)
				return q.text();
		}
		return null;
	}

	/* (non-Javadoc)
	 * @see com.ustadzmenjawab.crawler.Crawler#getArticle(java.lang.String)
	 */
	@Override
	public String getArticle(String body){
		
		Document doc = Jsoup.parse(body);
		Element p = doc.select("div[class*=text]").first();
		
		if(p != null)
			return p.text();
		return null;
	}
}
